package com.google.code.pdfbrick.merger;

import com.google.code.pdfbrick.engine.command.FilePageRangeParser;
import com.google.code.pdfbrick.engine.constant.CommandConstant;
import com.google.code.pdfbrick.engine.exception.CommandParseException;

/**
 * @author Johny Huang
 * @date 2009-3-13
 */
public class MergeCommandParser {
	
	public MergeCommandParser(){
	}

	/**
	 * <p>The merge command should has a pattern like </p>
	 * -merge desFilePath [srcFilePath&lt;PageRange,PageRange...&gt;;srcFilePath&lt;&gt;;...]
	 * </br><br/>
	 * <p>PageRange pattern example:
	 * <9,5-14,7-3><br/>
	 * 9 means the page 9.It's a PageRange of single page.<br/>
	 * 5-14 means from page 5(included) to page 14.It's a PageRange in order.<br/>
	 * 7-3 means from page 7(included) to page 3.It's a PageRange in reverse order.<br/>
	 * </p>
	 * 
	 * @param A String array which is split from a command line.
	 * @return
	 */
	public MergeCommandParams parse(final String[] args){		
		if(args==null){
			throw new NullPointerException("The param of the command should not be null!");
		}
		
		if(args.length<2){
			throw new CommandParseException("The number of the command params should be two!");
		}
		
		MergeCommandParams params=new MergeCommandParams();
		final String desFilePath=args[0];
		final String srcFilePaths=args[1];
		final String[] srcFilePathArray=srcFilePaths.split(CommandConstant.SEMICOLON);
		
		params.setDesFilePath(desFilePath);
		FilePageRangeParser fprParser=FilePageRangeParser.getInstance();
		for(String filePath:srcFilePathArray){				
			params.addFilePageRange(fprParser.parseFilePageRangeStr(filePath));
		}
				
		return params;
	}
	
}
